# -*- coding: utf-8 -*-
'''
@author: Hugo
@file: 爬唯品会手机.py
@time: 2021/8/8 17:36
'''
import asyncio
from lxml import etree
import pyppeteer


class wph_crawl():
    def __init__(self,name):
        self.base_url = 'https://category.vip.com/suggest.php?keyword='
        self.cookies = {

            'cookie':'vip_rip=120.229.96.219; vip_address=%257B%2522pname%2522%253A%2522%255Cu5e7f%255Cu4e1c%255Cu7701%2522%252C%2522cname%2522%253A%2522%255Cu6df1%255Cu5733%255Cu5e02%2522%252C%2522pid%2522%253A%2522104104%2522%252C%2522cid%2522%253A%2522104104103%2522%257D; vip_province=104104; vip_province_name=%E5%B9%BF%E4%B8%9C%E7%9C%81; vip_city_name=%E6%B7%B1%E5%9C%B3%E5%B8%82; vip_city_code=104104103; vip_wh=VIP_NH; user_class=a; mars_sid=546b317f8c252d8806e35a01457e01a1; mars_pid=0; visit_id=67184C3E2970C549C1E36B695F62F40D; PHPSESSID=nt6d7j82o7bmn5pu3h1kvieqp7; VipUINFO=luc%3Aa%7Csuc%3Aa%7Cbct%3Ac_new%7Chct%3Ac_new%7Cbdts%3A0%7Cbcts%3A0%7Ckfts%3A0%7Cc10%3A0%7Crcabt%3A0%7Cp2%3A0%7Cp3%3A1%7Cp4%3A0%7Cp5%3A0%7Cul%3A3105; vip_tracker_source_from=; pg_session_no=6; vip_access_times=%7B%22list%22%3A3%7D; mars_cid=1628415815274_abf4a95cc5fdd76c116fc5eb865348e6'
        }
        self.name = name
    async def ff(self):
        browser = await pyppeteer.launch(headless=False)
        page = await browser.newPage()
        await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36')
        await page.evaluateOnNewDocument(
            '() =>{ Object.defineProperties(navigator,''{ webdriver:{ get: () => false } })}')
        await page.goto(f'{self.base_url}/{self.name}')
        dimensions = await page.evaluate('''() => { return { cookie: window.document.cookie, } }''')
        print(f'cookies----->>{dimensions}')
        await asyncio.sleep(3)
        html = await page.content()
        data = etree.HTML(html).xpath('//div[contains(@class,"c-filter-group-scroll-brand")]/ul/li/a/@href')
        for i in data:
            url = f'http:{i}'
            print(url)


    async def run(self):
        await self.ff()

if __name__ == '__main__':
    s = wph_crawl('手机')
    asyncio.get_event_loop().run_until_complete(s.run())

